import { bus } from "../../network/index";
let token = wx.getStorageSync('token');
const app = getApp();
//Page Object
Page({
  data: {
    swiperList: [],
    value: "",
    sudokuList: [],
    shopList: []
  },
  search(event) {
    wx.navigateTo({
      url: '/pages/search/search?val=' + event.detail,
    });
  },
  //options(Object)
  onLoad: function (options) {
    this.getBanner();
    this.getSudoku();
    this.getShop();
  },
  getCartInfo() {
    wx.showLoading({
      title: 'loading...',
    });
    setTimeout(() => {
      app.globalData.init.then(() => {
        // console.log(token);
        bus.getCartInfo({ token }).then((res) => {
          // console.log(res.data)
          if (res.data.code == 0) {
            wx.setTabBarBadge({
              index: 2,
              text: String(res.data.data.number),
            });
            this.setData({
              isShow: false,
              list: res.data.data.items,
              price: res.data.data.price
            });
          } else if (res.data.code == 700) {
            wx.removeTabBarBadge({
              index: 2,
            });
            this.setData({
              isShow: true,
              list: []
            });
          }
          wx.hideLoading();
        }).catch((err) => {
          console.log(err);
        })
      })
    }, 1000);
    
  },
  getShop() {
    bus.goodsList().then((res) => {
      // console.log(res.data);
      this.setData({
        shopList: res.data.data
      })
    }).catch((err) => {
      console.log(err);
    })
  },

  getBanner() {
    bus.banner({ type: 'index' }).then((res) => {
      let data = res.data.data;
      this.setData({
        swiperList: data
      })
    }).catch((err) => {
      console.log(err);
    })
  },
  getSudoku() {
    bus.category().then((res) => {
      let data = res.data.data;
      this.setData({
        sudokuList: data
      })
      // console.log(this.data.sudokuList)
    }).catch((err) => {
      console.log(err);
    })
  },
  next(event) {
    let key = event.currentTarget.dataset.key;
    app.globalData.selectIndex = key
    wx.switchTab({
      url: '/pages/category/category',
    })
  },
  onReady: function () {

  },
  onShow: function () {
    this.getCartInfo()
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  onPageScroll: function () {

  },
  //item(index,pagePath,text)
  onTabItemTap: function (item) {

  }
});